home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / Choose.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  11KB  |  511 lines

  1. /*
  2. **    Choose.c
  3. **
  4. **    Routines that call the application support library (ASL).
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #ifndef _GLOBAL_H
  11. #include "Global.h"
  12. #endif
  13.  
  14. STATIC LONG
  15. GetDirEntryType(STRPTR Name)
  16. {
  17.     APTR OldWindowPtr;
  18.     LONG Result = 0;
  19.     BPTR FileLock;
  20.  
  21.     DisableDOSRequesters(&OldWindowPtr);
  22.  
  23.     if(FileLock = Lock(Name,SHARED_LOCK))
  24.     {
  25.         D_S(struct FileInfoBlock,FileInfo);
  26.  
  27.         if(Examine(FileLock,FileInfo))
  28.             Result = FileInfo->fib_DirEntryType;
  29.  
  30.         UnLock(FileLock);
  31.     }
  32.  
  33.     EnableDOSRequesters(OldWindowPtr);
  34.  
  35.     return(Result);
  36. }
  37.  
  38. STATIC struct FileRequester *
  39. SelectDrawerCommon(struct Window *Parent,STRPTR TitleText,STRPTR PositiveText,STRPTR DrawerName,LONG DrawerNameSize,BOOL SaveMode)
  40. {
  41.     struct FileRequester *DrawerRequester;
  42.     struct TagItem LocalTags[4+1+1],*TagPtr;
  43.     UBYTE LocalDrawerName[MAX_FILENAME_LENGTH];
  44.  
  45.     if(DrawerName[0])
  46.         LimitedStrcpy(sizeof(LocalDrawerName),LocalDrawerName,DrawerName);
  47.     else
  48.     {
  49.         if(!LocalGetCurrentDirName(LocalDrawerName,sizeof(LocalDrawerName)))
  50.             LocalDrawerName[0] = 0;
  51.     }
  52.  
  53.     GetDimensionTags(Parent,LocalTags);
  54.  
  55.     TagPtr = &LocalTags[4];
  56.  
  57.     if(TitleText)
  58.     {
  59.         TagPtr->ti_Tag    = ASLFR_TitleText;
  60.         TagPtr->ti_Data    = (ULONG)TitleText;
  61.  
  62.         TagPtr++;
  63.     }
  64.  
  65.     TagPtr->ti_Tag = TAG_DONE;
  66.  
  67.     if(!PositiveText)
  68.         PositiveText = LocaleString(MSG_GLOBAL_OPEN_TXT);
  69.  
  70.     if(DrawerRequester = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest,
  71.         ASLFR_Window,            Parent,
  72.         ASLFR_InitialDrawer,    LocalDrawerName,
  73.         ASLFR_PositiveText,        PositiveText,
  74.         ASLFR_Flags1,            SaveMode ? (FILF_SAVE | FILF_NEWIDCMP) : FILF_NEWIDCMP,
  75.         ASLFR_Flags2,            FIL1F_NOFILES,
  76.     TAG_MORE,LocalTags))
  77.     {
  78.         BOOL GotIt = FALSE;
  79.  
  80.         LT_LockWindow(Parent);
  81.  
  82.         if(AslRequest(DrawerRequester,NULL))
  83.         {
  84.             PutDimensionTags(NULL,DrawerRequester->fr_LeftEdge,DrawerRequester->fr_TopEdge,DrawerRequester->fr_Width,DrawerRequester->fr_Height);
  85.  
  86.             if(DrawerRequester->fr_Drawer[0])
  87.             {
  88.                 LimitedStrcpy(DrawerNameSize,DrawerName,DrawerRequester->fr_Drawer);
  89.                 GotIt = TRUE;
  90.             }
  91.         }
  92.  
  93.         if(!GotIt)
  94.         {
  95.             FreeAslRequest(DrawerRequester);
  96.             DrawerRequester = NULL;
  97.         }
  98.  
  99.         LT_UnlockWindow(Parent);
  100.     }
  101.  
  102.     return(DrawerRequester);
  103. }
  104.  
  105. struct FileRequester *
  106. SaveDrawer(struct Window *Parent,STRPTR TitleText,STRPTR PositiveText,STRPTR DrawerName,LONG DrawerNameSize)
  107. {
  108.     return(SelectDrawerCommon(Parent,TitleText,PositiveText,DrawerName,DrawerNameSize,TRUE));
  109. }
  110.  
  111. struct FileRequester *
  112. OpenDrawer(struct Window *Parent,STRPTR TitleText,STRPTR PositiveText,STRPTR DrawerName,LONG DrawerNameSize)
  113. {
  114.     return(SelectDrawerCommon(Parent,TitleText,PositiveText,DrawerName,DrawerNameSize,FALSE));
  115. }
  116.  
  117. struct FileRequester *
  118. SaveFile(struct Window *Parent,STRPTR TitleText,STRPTR PositiveText,STRPTR Pattern,STRPTR FullName,LONG FullNameSize)
  119. {
  120.     UBYTE LocalDrawerName[MAX_FILENAME_LENGTH];
  121.     struct TagItem LocalTags[4+2+1],*TagPtr;
  122.     struct FileRequester *FileRequester;
  123.     STRPTR DrawerName,FileName;
  124.     UBYTE Separator;
  125.     ULONG Flags;
  126.  
  127.     Flags = FILF_SAVE | FILF_NEWIDCMP;
  128.  
  129.     GetDimensionTags(Parent,LocalTags);
  130.  
  131.     TagPtr = &LocalTags[4];
  132.  
  133.     if(Pattern)
  134.     {
  135.         Flags |= FRF_DOPATTERNS;
  136.  
  137.         TagPtr->ti_Tag    = ASLFR_InitialPattern;
  138.         TagPtr->ti_Data    = (ULONG)Pattern;
  139.  
  140.         TagPtr++;
  141.     }
  142.  
  143.     if(TitleText)
  144.     {
  145.         TagPtr->ti_Tag    = ASLFR_TitleText;
  146.         TagPtr->ti_Data    = (ULONG)TitleText;
  147.  
  148.         TagPtr++;
  149.     }
  150.  
  151.     TagPtr->ti_Tag = TAG_DONE;
  152.  
  153.     if(!PositiveText)
  154.         PositiveText = LocaleString(MSG_GLOBAL_SAVE_TXT);
  155.  
  156.     if(GetDirEntryType(FullName) > 0)
  157.     {
  158.         FileName    = "";
  159.         DrawerName    = FullName;
  160.         Separator    = 0;
  161.     }
  162.     else
  163.     {
  164.         FileName    = FilePart(FullName);
  165.         DrawerName    = PathPart(FullName);
  166.         Separator    = *DrawerName;
  167.         *DrawerName    = 0;
  168.     }
  169.  
  170.     if(FullName[0])
  171.         LimitedStrcpy(sizeof(LocalDrawerName),LocalDrawerName,FullName);
  172.     else
  173.     {
  174.         if(!LocalGetCurrentDirName(LocalDrawerName,sizeof(LocalDrawerName)))
  175.             LocalDrawerName[0] = 0;
  176.     }
  177.  
  178.     if(Separator)
  179.         *DrawerName = Separator;
  180.  
  181.     FileRequester = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest,
  182.         ASLFR_Window,            Parent,
  183.         ASLFR_InitialDrawer,    LocalDrawerName,
  184.         ASLFR_InitialFile,        FileName,
  185.         ASLFR_PositiveText,        PositiveText,
  186.         ASLFR_Flags1,            Flags,
  187.     TAG_MORE,LocalTags);
  188.  
  189.     if(FileRequester)
  190.     {
  191.         BOOL GotIt = FALSE;
  192.  
  193.         LT_LockWindow(Parent);
  194.  
  195.         if(AslRequest(FileRequester,NULL))
  196.         {
  197.             PutDimensionTags(NULL,FileRequester->fr_LeftEdge,FileRequester->fr_TopEdge,FileRequester->fr_Width,FileRequester->fr_Height);
  198.  
  199.             if(FileRequester->fr_File[0] && strlen(FileRequester->fr_Drawer) < FullNameSize)
  200.             {
  201.                 LimitedStrcpy(FullNameSize,FullName,FileRequester->fr_Drawer);
  202.  
  203.                 if(AddPart(FullName,FileRequester->fr_File,FullNameSize))
  204.                     GotIt = TRUE;
  205.             }
  206.         }
  207.  
  208.         if(GotIt && Config->MiscConfig->ProtectiveMode)
  209.         {
  210.             BPTR FileLock;
  211.             BOOL DoesExist;
  212.  
  213.             if(FileLock = Lock(FullName,SHARED_LOCK))
  214.             {
  215.                 DoesExist = TRUE;
  216.  
  217.                 UnLock(FileLock);
  218.             }
  219.             else
  220.                 DoesExist = (BOOL)(IoErr() == ERROR_OBJECT_IN_USE);
  221.  
  222.             if(DoesExist)
  223.             {
  224.                 if(!ShowRequest(Parent,LocaleString(MSG_GLOBAL_FILE_ALREADY_EXISTS_OVERWRITE_TXT),LocaleString(MSG_GLOBAL_REPLACE_CANCEL_TXT),FilePart(FullName)))
  225.                     GotIt = FALSE;
  226.             }
  227.         }
  228.  
  229.         if(!GotIt)
  230.         {
  231.             FreeAslRequest(FileRequester);
  232.             FileRequester = NULL;
  233.         }
  234.  
  235.         LT_UnlockWindow(Parent);
  236.     }
  237.  
  238.     return(FileRequester);
  239. }
  240.  
  241. struct FileRequester *
  242. OpenSingleFile(struct Window *Parent,STRPTR TitleText,STRPTR PositiveText,STRPTR Pattern,STRPTR FullName,LONG FullNameSize)
  243. {
  244.     UBYTE LocalDrawerName[MAX_FILENAME_LENGTH];
  245.     struct TagItem LocalTags[4+2+1],*TagPtr;
  246.     struct FileRequester *FileRequester;
  247.     STRPTR DrawerName,FileName;
  248.     UBYTE Separator;
  249.     ULONG Flags;
  250.  
  251.     Flags = FILF_NEWIDCMP;
  252.  
  253.     GetDimensionTags(Parent,LocalTags);
  254.  
  255.     TagPtr = &LocalTags[4];
  256.  
  257.     if(Pattern)
  258.     {
  259.         Flags |= FRF_DOPATTERNS;
  260.  
  261.         TagPtr->ti_Tag    = ASLFR_InitialPattern;
  262.         TagPtr->ti_Data    = (ULONG)Pattern;
  263.  
  264.         TagPtr++;
  265.     }
  266.  
  267.     if(TitleText)
  268.     {
  269.         TagPtr->ti_Tag    = ASLFR_TitleText;
  270.         TagPtr->ti_Data    = (ULONG)TitleText;
  271.  
  272.         TagPtr++;
  273.     }
  274.  
  275.     TagPtr->ti_Tag = TAG_DONE;
  276.  
  277.     if(!PositiveText)
  278.         PositiveText = LocaleString(MSG_GLOBAL_OPEN_TXT);
  279.  
  280.     if(GetDirEntryType(FullName) > 0)
  281.     {
  282.         FileName    = "";
  283.         DrawerName    = FullName;
  284.         Separator    = 0;
  285.     }
  286.     else
  287.     {
  288.         FileName    = FilePart(FullName);
  289.         DrawerName    = PathPart(FullName);
  290.         Separator    = *DrawerName;
  291.         *DrawerName    = 0;
  292.     }
  293.  
  294.     if(FullName[0])
  295.         LimitedStrcpy(sizeof(LocalDrawerName),LocalDrawerName,FullName);
  296.     else
  297.     {
  298.         if(!LocalGetCurrentDirName(LocalDrawerName,sizeof(LocalDrawerName)))
  299.             LocalDrawerName[0] = 0;
  300.     }
  301.  
  302.     if(Separator)
  303.         *DrawerName = Separator;
  304.  
  305.     FileRequester = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest,
  306.         ASLFR_Window,            Parent,
  307.         ASLFR_InitialDrawer,    LocalDrawerName,
  308.         ASLFR_InitialFile,        FileName,
  309.         ASLFR_PositiveText,        PositiveText,
  310.         ASLFR_Flags1,            Flags,
  311.     TAG_MORE,LocalTags);
  312.  
  313.     if(FileRequester)
  314.     {
  315.         BOOL GotIt = FALSE;
  316.  
  317.         LT_LockWindow(Parent);
  318.  
  319.         if(AslRequest(FileRequester,NULL))
  320.         {
  321.             PutDimensionTags(NULL,FileRequester->fr_LeftEdge,FileRequester->fr_TopEdge,FileRequester->fr_Width,FileRequester->fr_Height);
  322.  
  323.             if(FileRequester->fr_File[0] && strlen(FileRequester->fr_Drawer) < FullNameSize)
  324.             {
  325.                 LimitedStrcpy(FullNameSize,FullName,FileRequester->fr_Drawer);
  326.  
  327.                 if(AddPart(FullName,FileRequester->fr_File,FullNameSize))
  328.                     GotIt = TRUE;
  329.             }
  330.         }
  331.  
  332.         if(!GotIt)
  333.         {
  334.             FreeAslRequest(FileRequester);
  335.             FileRequester = NULL;
  336.         }
  337.  
  338.         LT_UnlockWindow(Parent);
  339.     }
  340.  
  341.     return(FileRequester);
  342. }
  343.  
  344. struct FileRequester *
  345. OpenSeveralFiles(struct Window *Parent,STRPTR TitleText,STRPTR PositiveText,STRPTR Pattern,STRPTR FullName,LONG FullNameSize)
  346. {
  347.     UBYTE LocalDrawerName[MAX_FILENAME_LENGTH];
  348.     struct TagItem LocalTags[4+3+1],*TagPtr;
  349.     struct FileRequester *FileRequester;
  350.     STRPTR DrawerName,FileName;
  351.     UBYTE Separator;
  352.     ULONG Flags;
  353.  
  354.     Flags = FILF_MULTISELECT | FILF_NEWIDCMP;
  355.  
  356.     GetDimensionTags(Parent,LocalTags);
  357.  
  358.     TagPtr = &LocalTags[4];
  359.  
  360.     if(Pattern)
  361.     {
  362.         Flags |= FRF_DOPATTERNS;
  363.  
  364.         TagPtr->ti_Tag    = ASLFR_InitialPattern;
  365.         TagPtr->ti_Data    = (ULONG)Pattern;
  366.  
  367.         TagPtr++;
  368.     }
  369.  
  370.     if(TitleText)
  371.     {
  372.         TagPtr->ti_Tag    = ASLFR_TitleText;
  373.         TagPtr->ti_Data    = (ULONG)TitleText;
  374.  
  375.         TagPtr++;
  376.     }
  377.  
  378.     if(PositiveText)
  379.     {
  380.         TagPtr->ti_Tag    = ASLFR_PositiveText;
  381.         TagPtr->ti_Data    = (ULONG)PositiveText;
  382.  
  383.         TagPtr++;
  384.     }
  385.  
  386.     TagPtr->ti_Tag = TAG_DONE;
  387.  
  388.     if(GetDirEntryType(FullName) > 0)
  389.     {
  390.         FileName    = "";
  391.         DrawerName    = FullName;
  392.         Separator    = 0;
  393.     }
  394.     else
  395.     {
  396.         FileName    = FilePart(FullName);
  397.         DrawerName    = PathPart(FullName);
  398.         Separator    = *DrawerName;
  399.         *DrawerName    = 0;
  400.     }
  401.  
  402.     if(FullName[0])
  403.         LimitedStrcpy(sizeof(LocalDrawerName),LocalDrawerName,FullName);
  404.     else
  405.     {
  406.         if(!LocalGetCurrentDirName(LocalDrawerName,sizeof(LocalDrawerName)))
  407.             LocalDrawerName[0] = 0;
  408.     }
  409.  
  410.     if(Separator)
  411.         *DrawerName = Separator;
  412.  
  413.     FileRequester = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest,
  414.         ASLFR_Window,            Parent,
  415.         ASLFR_InitialDrawer,    LocalDrawerName,
  416.         ASLFR_InitialFile,        FileName,
  417.         ASLFR_Flags1,            Flags,
  418.     TAG_MORE,LocalTags);
  419.  
  420.     if(FileRequester)
  421.     {
  422.         BOOL GotIt = FALSE;
  423.  
  424.         LT_LockWindow(Parent);
  425.  
  426.         if(AslRequest(FileRequester,NULL))
  427.         {
  428.             PutDimensionTags(NULL,FileRequester->fr_LeftEdge,FileRequester->fr_TopEdge,FileRequester->fr_Width,FileRequester->fr_Height);
  429.  
  430.             if(strlen(FileRequester->fr_Drawer) < FullNameSize)
  431.             {
  432.                 STRPTR LocalFileName;
  433.  
  434.                 LimitedStrcpy(FullNameSize,FullName,FileRequester->fr_Drawer);
  435.  
  436.                 if(FileRequester->fr_NumArgs > 1 && FileRequester->fr_ArgList != NULL)
  437.                     LocalFileName = FileRequester->fr_ArgList->wa_Name;
  438.                 else
  439.                     LocalFileName = FileRequester->fr_File;
  440.  
  441.                 if(LocalFileName[0])
  442.                 {
  443.                     if(AddPart(FullName,LocalFileName,FullNameSize))
  444.                         GotIt = TRUE;
  445.                 }
  446.             }
  447.         }
  448.  
  449.         if(!GotIt)
  450.         {
  451.             FreeAslRequest(FileRequester);
  452.             FileRequester = NULL;
  453.         }
  454.  
  455.         LT_UnlockWindow(Parent);
  456.     }
  457.  
  458.     return(FileRequester);
  459. }
  460.  
  461. STATIC BOOL
  462. OpenFontCommon(struct Window *Parent,STRPTR FontName,WORD *FontSize,BOOL MonoSpaced)
  463. {
  464.     struct TagItem             DimensionTags[5];
  465.     struct FontRequester    *FontRequester;
  466.     BOOL                     Result = FALSE;
  467.  
  468.     if(FontRequester = (struct FontRequester *)AllocAslRequestTags(ASL_FontRequest,
  469.         ASLFO_Window,            Parent,
  470.         ASLFO_InitialName,        FontName,
  471.         ASLFO_InitialSize,        *FontSize,
  472.         ASLFO_InitialFrontPen,    Pens[TEXTPEN],
  473.         ASLFO_InitialBackPen,    Pens[BACKGROUNDPEN],
  474.         ASLFO_PrivateIDCMP,        TRUE,
  475.         ASLFO_MaxHeight,        255,
  476.         ASL_FuncFlags,            MonoSpaced ? (FONF_NEWIDCMP|FONF_FIXEDWIDTH) : FONF_NEWIDCMP,
  477.     TAG_MORE,GetDimensionTags(NULL,DimensionTags)))
  478.     {
  479.         LT_LockWindow(Parent);
  480.  
  481.         if(AslRequest(FontRequester,NULL))
  482.         {
  483.             PutDimensionTags(NULL,FontRequester->fo_LeftEdge,FontRequester->fo_TopEdge,FontRequester->fo_Width,FontRequester->fo_Height);
  484.  
  485.             strcpy(FontName,FontRequester->fo_Attr.ta_Name);
  486.  
  487.             *FontSize = FontRequester->fo_Attr.ta_YSize;
  488.  
  489.             Result = TRUE;
  490.         }
  491.  
  492.         LT_UnlockWindow(Parent);
  493.  
  494.         FreeAslRequest(FontRequester);
  495.     }
  496.  
  497.     return(Result);
  498. }
  499.  
  500. BOOL
  501. OpenAnyFont(struct Window *Parent,STRPTR FontName,WORD *FontSize)
  502. {
  503.     return(OpenFontCommon(Parent,FontName,FontSize,FALSE));
  504. }
  505.  
  506. BOOL
  507. OpenFixedFont(struct Window *Parent,STRPTR FontName,WORD *FontSize)
  508. {
  509.     return(OpenFontCommon(Parent,FontName,FontSize,TRUE));
  510. }
  511.